import request from '@/utils/request'
import { appId } from '@/utils/config'
import axios from "axios";
export function checkUser() {
	return request({
		url: '/user/check',
		method: 'get',
	})
}

export function loginUser(loginCode) {
	return request({
		url: '/user/login',
		method: 'post',
		data: { 
			code: loginCode,
			appId: appId,
		},
	})
}

export function registerUser(registerUser) {
	if(uni.getStorageSync('recommender')) {
		registerUser.recommender = uni.getStorageSync('recommender');
	}
	registerUser.appId = appId;
	return request({
		url: '/user/register',
		method: 'post',
		data: registerUser,
	})
}

export function getPhoneNumber(code) {
	return request({
		url: '/user/get-phone-number',
		method: 'get',
		data: { code: code },
	})
}

export function getUser() {
	return request({
		url: '/user',
		method: 'get',
	})
}
export function getQiniuToken() {
  return request({
    url: '/common/getQiniuToken',
    method: 'get'
  })
}
export function saveFileInfo(query) {
  return request({
    url: '/common/saveFileInfo',
    method: 'get',
    params: query
  })
}
export async function updateUser(isAvatarFile, user, token) {
	if(isAvatarFile) {
		user.attachmentId = await new Promise((resolve, reject) => {
			// uni.uploadFile({
			// 	url: 'https://www.ym-cloud.cn/wxapi/common/upload', //仅为示例，非真实的接口地址
			// 	//url:'http://localhost:8080/wxapi/common/upload',
			// 	filePath: user.avatarUrl,
			// 	name: 'file',
			// 	header: {
			// 		Authorization: `Bearer ${token}`,
			// 	},
			// 	success: (uploadFileRes) => {
			// 		resolve(JSON.parse(uploadFileRes.data).fileId);
			// 	}
			// });
			    getQiniuToken().then((result) => {
			      //获取token
			      let token = result.data;
				          const key = `front_${new Date().getTime()}${Math.floor(
				            Math.random() * 100
				          )}.`+user.avatarUrl.split(".")[1]; //自定义图片名
						  uni.uploadFile({
						  	url: 'https://up-z1.qiniup.com', 
						  	filePath: user.avatarUrl,
						  	name: 'file',
						  	    formData: {
						  	      'token': token,
								  'key':"image/" + key
						  	    },
						  	success: (res) => {
								let params = {
								    fileName: key,
								    saveName: key,
								    fileSize: JSON.parse(res.data).fsize,
								    fileUrl: JSON.parse(res.data).key,
								};
								saveFileInfo(params).then((res) => {
								  resolve(res.data);
								});
						  		//resolve(JSON.parse(uploadFileRes.data).fileId);
						  	}
						  });
			    });
		});
	}
	return request({
		url: '/user/update',
		method: 'post',
		data: user,
	})
}

export function listUserService() {
	return request({
		url: '/user/service',
		method: 'get',
	})
}

// 用户订单
export function getOrderStatusNum(orderStatus) {
	return request({
		url: '/user/order/status-num',
		method: 'get',
		params: {
			orderStatus
		}
	})
}

export function listUserOrder(orderStatus) {
	return request({
		url: '/user/order/list',
		method: 'get',
		params: {
			orderStatus
		}
	})
}

// 用户地址
export function listUserAddress() {
	return request({
		url: '/user/address/list',
		method: 'get',
	})
}

export function getAddress(addressId) {
	return request({
		url: '/user/address',
		method: 'get',
		params: { addressId }
	})
}

export function getDefaultAddress() {
	return request({
		url: '/user/address/get-default',
		method: 'get',
	})
}

export function setDefaultAddress(addressId) {
	return request({
		url: '/user/address/set-default',
		method: 'get',
		params: { addressId }
	})
}

export function editUserAddress(address) {
	return request({
		url: '/user/address/edit',
		method: 'post',
		data: address
	})
}

export function deleteUserAddress(addressId) {
	return request({
		url: '/user/address/delete',
		method: 'get',
		params: { addressId }
	})
}

export function addUserAddress(addressForm) {
	return request({
		url: '/user/address/add',
		method: 'post',
		data: addressForm
	})
}

// 用户充值
export function listRechargePlan() {
	return request({
		url: '/user/wallent/plan',
		method: 'get',
	})
}

export function addRecharge(recharge, appId) {
	return request({
		url: '/user/wallent/prepay/' + appId,
		method: 'post',
		data: recharge
	})
}

export function rechargeSuccess(orderId) {
	return request({
		url: '/user/wallent/pay-success',
		method: 'get',
		params: { orderId }
	})
}

export function rechargeFail(orderId) {
	return request({
		url: '/user/wallent/pay-fail',
		method: 'get',
		params: { orderId }
	})
}

// 用户分享
export function getPosterQrcode() {
	return request({
		url: '/user/poster-qrcode',
		method: 'get',
	})
}

// 用户折扣
export function getUserDiscount() {
	return request({
		url: '/user/discount',
		method: 'get',
		params: {
			brandId: uni.getStorageSync("brandId")
		}
	})
}

// 用户优惠券
export function listUserCoupon(couponStatus) {
	return request({
		url: '/user/coupon',
		method: 'get',
		params: { couponStatus }
	})
}

// 用户佣金
export function listUserCommission() {
	return request({
		url: '/user/commission',
		method: 'get',
	})
}